<?php
namespace api\home\controller;

use cmf\controller\RestUserBaseController;
use app\common\model\MaterialModel;
use app\common\model\MaterialClassModel;
use app\common\logic\home\Material;

//素材
class MaterialController extends RestUserBaseController
{

	/**
	 * [获取个人是共享素材列表]
	 * @param [type] $param.id          [课题ID]
	 * @param [type] $param.start_time          [开始时间]
	 * @param [type] $param.end_time          [结束时间]
	 * @return [type] [description]
	 */
	public function getList(Material $Material){
	
		$param = $this->request->param();
		$data_material_class = MaterialClassModel::where('user_id',$this->userId)->where('mytask_id',$param['id'])->field('material_id')->select()->toArray();
		$array = [];
		foreach ($data_material_class as $key => $value) {
			$array[$key] = $value['material_id'];
		}
		//dump($array);

		$data = $Material->getList($this->userId,$param)->toArray();
		
		if(!empty($data['data'])){
	
			foreach ($data['data'] as $key => $value) {
				if(in_array($value['id'], $array)){

					$data['data'][$key]['sta'] = 0;
				}else{
					$data['data'][$key]['sta'] = 1;
				}
				
			}
		}
	
		if(!empty($data['data'])){
			$this->success('成功',$data);
		}else{
			$this->error('没有数据');
		}
	}
	/**
	 * [设置某素材的同组成员]
	 * @param [type] $param.material_id [素材ID]
	 * @param [type] $param.id          [课题ID]
	 */
	public function setMaterial(Material $Material){
		$param = $this->request->param();
		$rs = $Material->setMaterial($param['material_id'],$param['id'],$this->userId);
		if($rs){
			$this->success('成功');
		}else{
			$this->error('失败');
		}
	}
	/**
	 * [获取该素材的同一组]
	 * @param [type] $[id] [课题ID]
	 * @return [type] [description]
	 */
	public function group(Material $Material){
		$param = $this->request->param();
		
		$data = $Material->group($param['id'],$this->userId);
		if(empty($data)){
			$this->error('空');
		}else{
			$this->success('成功',$data);
		}
	}
	/**
	 * [素材隐藏]
	 * @param [type] $[id] [素材ID]
	 * @param [type] $[show_status] [1 显示 2隐藏]
	 * @return [type] [description]
	 */
	public function set(){
		$param = $this->request->param();
		if(empty($param['id']) || empty($param['show_status']) ){
			$this->error('没有传输id或素材状态show_status');
		}
		$rs = MaterialModel::where('id',$param['id'])->where('userId',$this->userId)->update(['show_status'=>$param['show_status']]);
		if($rs){
			$this->success('成功');
		}else{
			$this->error('失败');
		}
	}
	/**
	 * [素材删除]
	 * @param [type] $[id] [素材ID]
	 * @return [type] [description]
	 */
	public function delete(){
		$param = $this->request->param();
		if(empty($param)){
			$this->error('没有传输ID');
		}
		$rs = MaterialModel::where('id',$param['id'])->where('userId',$this->userId)->update(['delete_time'=>date('Y-m-d H:i:s')]);
		if($rs){
			$this->success('成功');
		}else{
			$this->error('失败');
		}
	}
}
